﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.IO.IsolatedStorage;

namespace wallstreet
{
    public partial class Control_News : PhoneApplicationPage
    {
        public event EventHandler goToNewsView;

        public Control_News()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //loadInfo();
        }

        public void loadInfo()
        {
            lboNews.ItemsSource = null;

            WebClient _client = new WebClient();
            _client.DownloadStringCompleted += ReadNews;
            _client.DownloadStringAsync(new Uri(("http://feeds.finance.yahoo.com/rss/2.0/category-international?region=US&lang=en-US")));
        }

        private void ReadNews(object Sender, DownloadStringCompletedEventArgs e)
        {
            List<NewsFeed> newsList = new List<NewsFeed>();

            try
            {
                if (!e.Cancelled)
                {
                    XElement xml = XElement.Parse(e.Result);
                    foreach (XElement value in xml.Elements("channel").Elements("item"))
                    {
                        newsList.Add(new NewsFeed
                        {
                            Title = value.Element("title").Value,
                            Description = Regex.Replace(value.Element("description").Value, @"<(.|\n)*?>", String.Empty),
                            Link = value.Element("link").Value,
                            Guid = value.Element("guid").Value,
                            Published = value.Element("pubDate").Value
                        });
                    }

                    newsList.Add(new NewsFeed { Title = "" });

                    lboNews.ItemsSource = newsList;
                }
            }
            catch(Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
        }

        private void lboNews_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NewsFeed feed = lboNews.SelectedItem as NewsFeed;

            if (feed != null)
            {
                if (feed.Link != "")
                {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("NewsFeed"))
                    IsolatedStorageSettings.ApplicationSettings["NewsFeed"] = feed;
                else
                    IsolatedStorageSettings.ApplicationSettings.Add("NewsFeed", feed);

                goToNewsView(null, new EventArgs());
            }

            lboNews.SelectedIndex = -1;
        }
        }
    }

    public class NewsFeed
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Link { get; set; }
        public string Guid { get; set; }
        public string Published { get; set; }
    }
}